package com.wyh.controller.gfive;

import com.wyh.common.result.Result;
import com.wyh.domain.po.gfive.Journal;
import com.wyh.domain.po.gfive.PageHelper;
import com.wyh.service.gfive.JournalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/journal")
@Api(tags = "系统管理-日志查询")
public class SysJournalController {
    @Resource
    private JournalService journalService;

    @GetMapping("/all")
    @ApiOperation("分页查询所有日志")
    public Result<Map<String, Object>> getAllJournals(@RequestParam(required = false) @ApiParam("页数") Integer currentPage,
                                                      @RequestParam(required = false) @ApiParam("条数") Integer pageSize) {
        int page = (currentPage == null) ? 1 : currentPage;
        int size = (pageSize == null) ? 10 : pageSize;
        PageHelper pageHelper = new PageHelper(page, size);

        return journalService.searchJournals(null, null, null, null, pageHelper);
    }

    @GetMapping("/search")
    @ApiOperation("条件查询")
    public   Result<Map<String, Object>> searchJournals(
            @RequestParam(required = false) @ApiParam("用户名") String user,
            @RequestParam(required = false) @ApiParam("操作功能") String function,
            @RequestParam(required = false) @ApiParam("开始日期") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @ApiParam("截止日期") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            @RequestParam(required = false) @ApiParam("页数") Integer currentPage,
            @RequestParam(required = false) @ApiParam("条数") Integer pageSize) {

        LocalDateTime startTime = null;
        LocalDateTime endTime = null;

        if (startDate != null) {
            startTime = startDate.atStartOfDay();
        }
        if (endDate != null) {
            endTime = endDate.atTime(23, 59, 59);
        }

        int page = (currentPage == null) ? 1 : currentPage;
        int size = (pageSize == null) ? 10 : pageSize;
        PageHelper pageHelper = new PageHelper(page, size);
        return journalService.searchJournals(user, function, startTime, endTime, pageHelper);
    }

    @PostMapping("/add")
    @ApiOperation("新增日志")
    public Result<Journal> addJournal(@RequestBody @ApiParam("日志表") Journal journal) {
         journalService.save(journal);
        return Result.success(journal);
    }

}
